import { Monaco } from '@monaco-editor/react';

// 定义模型和位置的接口，以解决类型问题
interface IModel {
  getWordUntilPosition: (position: IPosition) => { 
    startColumn: number; 
    endColumn: number; 
  };
}

interface IPosition {
  lineNumber: number;
  column: number;
}

// 注册代码补全提供程序
export const registerCompletionProviders = (monaco: Monaco, completionEnabled: boolean): any[] => {
  if (!completionEnabled) return [];
  
  const providers = [];

  // 配置C++代码补全
  const cppProvider = monaco.languages.registerCompletionItemProvider('cpp', {
    provideCompletionItems: (model: IModel, position: IPosition) => {
      const word = model.getWordUntilPosition(position);
      const range = {
        startLineNumber: position.lineNumber,
        endLineNumber: position.lineNumber,
        startColumn: word.startColumn,
        endColumn: word.endColumn
      };

      const suggestions = [
        {
          label: 'cout',
          kind: monaco.languages.CompletionItemKind.Snippet,
          insertText: 'std::cout << ${1:} << std::endl;',
          insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
          documentation: '输出到控制台',
          range: range
        },
        {
          label: 'cin',
          kind: monaco.languages.CompletionItemKind.Snippet,
          insertText: 'std::cin >> ${1:};',
          insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
          documentation: '从控制台读取输入',
          range: range
        },
        {
          label: 'for',
          kind: monaco.languages.CompletionItemKind.Snippet,
          insertText: 'for (int ${1:i} = 0; ${1:i} < ${2:n}; ${1:i}++) {\n\t${3:}\n}',
          insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
          documentation: 'For循环',
          range: range
        },
        {
          label: 'while',
          kind: monaco.languages.CompletionItemKind.Snippet,
          insertText: 'while (${1:condition}) {\n\t${2:}\n}',
          insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
          documentation: 'While循环',
          range: range
        },
        {
          label: 'vector',
          kind: monaco.languages.CompletionItemKind.Snippet,
          insertText: 'std::vector<${1:int}> ${2:vec};',
          insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
          documentation: '创建向量',
          range: range
        },
        {
          label: 'map',
          kind: monaco.languages.CompletionItemKind.Snippet,
          insertText: 'std::map<${1:key}, ${2:value}> ${3:map};',
          insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
          documentation: '创建映射',
          range: range
        },
        {
          label: 'if',
          kind: monaco.languages.CompletionItemKind.Snippet,
          insertText: 'if (${1:condition}) {\n\t${2:}\n}',
          insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
          documentation: 'If语句',
          range: range
        },
        {
          label: 'ifelse',
          kind: monaco.languages.CompletionItemKind.Snippet,
          insertText: 'if (${1:condition}) {\n\t${2:}\n} else {\n\t${3:}\n}',
          insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
          documentation: 'If-Else语句',
          range: range
        },
        {
          label: 'class',
          kind: monaco.languages.CompletionItemKind.Snippet,
          insertText: 'class ${1:Name} {\npublic:\n\t${2:Name}() {}\n\t~${2:Name}() {}\n\nprivate:\n\t${3:}\n};',
          insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
          documentation: '创建类',
          range: range
        },
        {
          label: 'include',
          kind: monaco.languages.CompletionItemKind.Snippet,
          insertText: '#include <${1:iostream}>',
          insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
          documentation: '包含头文件',
          range: range
        }
      ];

      return { suggestions: suggestions };
    }
  });
  providers.push(cppProvider);

  // 配置Java代码补全
  const javaProvider = monaco.languages.registerCompletionItemProvider('java', {
    provideCompletionItems: (model: IModel, position: IPosition) => {
      const word = model.getWordUntilPosition(position);
      const range = {
        startLineNumber: position.lineNumber,
        endLineNumber: position.lineNumber,
        startColumn: word.startColumn,
        endColumn: word.endColumn
      };

      const suggestions = [
        {
          label: 'sout',
          kind: monaco.languages.CompletionItemKind.Snippet,
          insertText: 'System.out.println(${1:});',
          insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
          documentation: '打印到控制台',
          range: range
        },
        {
          label: 'for',
          kind: monaco.languages.CompletionItemKind.Snippet,
          insertText: 'for (int ${1:i} = 0; ${1:i} < ${2:n}; ${1:i}++) {\n\t${3:}\n}',
          insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
          documentation: 'For循环',
          range: range
        },
        {
          label: 'if',
          kind: monaco.languages.CompletionItemKind.Snippet,
          insertText: 'if (${1:condition}) {\n\t${2:}\n}',
          insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
          documentation: 'If语句',
          range: range
        },
        {
          label: 'class',
          kind: monaco.languages.CompletionItemKind.Snippet,
          insertText: 'public class ${1:Name} {\n\t${2:}\n}',
          insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
          documentation: '创建类',
          range: range
        }
      ];

      return { suggestions: suggestions };
    }
  });
  providers.push(javaProvider);

  // 配置Python代码补全
  const pythonProvider = monaco.languages.registerCompletionItemProvider('python', {
    provideCompletionItems: (model: IModel, position: IPosition) => {
      const word = model.getWordUntilPosition(position);
      const range = {
        startLineNumber: position.lineNumber,
        endLineNumber: position.lineNumber,
        startColumn: word.startColumn,
        endColumn: word.endColumn
      };

      const suggestions = [
        {
          label: 'print',
          kind: monaco.languages.CompletionItemKind.Snippet,
          insertText: 'print(${1:})',
          insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
          documentation: '打印到控制台',
          range: range
        },
        {
          label: 'for',
          kind: monaco.languages.CompletionItemKind.Snippet,
          insertText: 'for ${1:item} in ${2:items}:\n\t${3:pass}',
          insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
          documentation: 'For循环',
          range: range
        },
        {
          label: 'if',
          kind: monaco.languages.CompletionItemKind.Snippet,
          insertText: 'if ${1:condition}:\n\t${2:pass}',
          insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
          documentation: 'If语句',
          range: range
        },
        {
          label: 'def',
          kind: monaco.languages.CompletionItemKind.Snippet,
          insertText: 'def ${1:function_name}(${2:parameters}):\n\t${3:pass}',
          insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
          documentation: '定义函数',
          range: range
        },
        {
          label: 'class',
          kind: monaco.languages.CompletionItemKind.Snippet,
          insertText: 'class ${1:ClassName}:\n\tdef __init__(self, ${2:parameters}):\n\t\t${3:pass}',
          insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
          documentation: '定义类',
          range: range
        }
      ];

      return { suggestions: suggestions };
    }
  });
  providers.push(pythonProvider);

  // 配置JavaScript代码补全
  const jsProvider = monaco.languages.registerCompletionItemProvider('javascript', {
    provideCompletionItems: (model: IModel, position: IPosition) => {
      const word = model.getWordUntilPosition(position);
      const range = {
        startLineNumber: position.lineNumber,
        endLineNumber: position.lineNumber,
        startColumn: word.startColumn,
        endColumn: word.endColumn
      };

      const suggestions = [
        {
          label: 'log',
          kind: monaco.languages.CompletionItemKind.Snippet,
          insertText: 'console.log(${1:});',
          insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
          documentation: '打印到控制台',
          range: range
        },
        {
          label: 'for',
          kind: monaco.languages.CompletionItemKind.Snippet,
          insertText: 'for (let ${1:i} = 0; ${1:i} < ${2:array}.length; ${1:i}++) {\n\t${3:}\n}',
          insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
          documentation: 'For循环',
          range: range
        },
        {
          label: 'function',
          kind: monaco.languages.CompletionItemKind.Snippet,
          insertText: 'function ${1:name}(${2:params}) {\n\t${3:}\n}',
          insertTextRules: monaco.languages.CompletionItemInsertTextRule.InsertAsSnippet,
          documentation: '创建函数',
          range: range
        }
      ];

      return { suggestions: suggestions };
    }
  });
  providers.push(jsProvider);

  return providers;
};

// 代码补全相关的编辑器选项
export const getCompletionEditorOptions = (isEnabled: boolean) => {
  return {
    suggestOnTriggerCharacters: isEnabled,
    quickSuggestions: isEnabled ? { other: true, comments: true, strings: true } : false,
    parameterHints: { enabled: isEnabled },
    // 新版本Monaco (0.52.2+) 支持的选项
    inlineSuggest: { enabled: isEnabled },
    suggest: { showKeywords: isEnabled },
  };
}; 